package br.com.financas.config;

import javax.annotation.Resource;
import javax.inject.Inject;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
@ComponentScan(basePackages={"br.com.financas.business", "br.com.financas.facade", "br.com.financas.controller", "br.com.financas.model", "br.com.financas.repository"}, excludeFilters = { @Filter(Configuration.class) })
@PropertySource("classpath:application.properties")
public class SpringConfig {

	@Inject
	private Environment environment;

	@Resource(mappedName = "jdbc/maiscontrole")
	private DataSource dataSource;

	@Bean
	public DataSource dataSource(){
		return this.dataSource;
	}

	@Bean
	public JdbcTemplate jdbcTemplate() throws Exception{
		if(this.dataSource == null){
			this.dataSource();
		}
		return new JdbcTemplate(this.dataSource);
	}

	@Bean
	public PlatformTransactionManager transactionManager() throws Exception{
		if(this.dataSource == null){
			this.dataSource();
		}
		DataSourceTransactionManager tm = new DataSourceTransactionManager(this.dataSource);
		return tm;
	}
}
